package com.lft07.status;

/**
 * Function: 		A {@code TestState} 观察测试线程的状态<br/>
 * Reason:   		ADD REASON.<br/>
 * Date:            2021-01-27 14:58<br/>
 * <p>
 * Class Name:      TestState<br/>
 * Package Name:    com.lft07.status<br/>
 * @author Laifengting / E-mail:laifengting@foxmail.com
 * @version 1.0.0
 * @since JDK 8
 */
public class TestState {
	public static void main(String[] args) {
		Thread thread = new Thread(() -> {
			for (int i = 0; i < 5; i++) {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			System.out.println("///////////");
		});
		
		// 观察状态
		Thread.State state = thread.getState();
		System.out.println(state);
		
		// 观察启动后状态
		thread.start();
		state = thread.getState();
		System.out.println(state);
		
		// 只要线程不终止，就一直输出状态
		while (state != Thread.State.TERMINATED) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			// 更新线程状态
			state = thread.getState();
			// 打印输出状态
			System.out.println(state);
		}
		
		// 一旦进入死亡状态，就不能再次启动该线程。
		// thread.start();
		
		
	}
}
